package com.yaha.controler.db.interfaces.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import com.yaha.controler.db.DatabaseAdapter;
import com.yaha.controler.db.interfaces.DeviceHistoryList;

/**
 * <功能描述>存取设备连接历史记录的实现
 * 
 * @author Frewen.W/909091
 * @version [版本号, 2015-4-10]
 * @since [产品/模块版本]
 */
public class DeviceHistoryListImpl implements DeviceHistoryList {
	private static final String TAG = DeviceHistoryListImpl.class.getSimpleName();

	private Context mContext;
	private DatabaseAdapter mDatabaseAdapter;

	public DeviceHistoryListImpl(Context context) {
		this.mContext = context;
		this.mDatabaseAdapter = new DatabaseAdapter(mContext);
		this.mDatabaseAdapter.open();
	}

	@Override
	public long insert(String deviceName, String wifiName, long connectTime, int connectCount) {
		long row = -1;
		if (!TextUtils.isEmpty(deviceName) & !TextUtils.isEmpty(wifiName)) {
			ContentValues cv = new ContentValues();
			cv.put(DEVICE_NAME, deviceName);
			cv.put(WIFI_NAME, wifiName);
			cv.put(CONNECTED_TIME, connectTime);
			cv.put(CONNECT_COUNT, connectCount);
			row = this.mDatabaseAdapter.insert(TABLE_NAME, cv);
		}
		return row;
	}

	@Override
	public boolean update(String deviceName, String wifiName, long connectTime, int connectCount) {
		boolean isUpdate = false;
		if (!TextUtils.isEmpty(deviceName) && !TextUtils.isEmpty(wifiName)) {
			ContentValues cv = new ContentValues();
			cv.put(DEVICE_NAME, deviceName);
			cv.put(WIFI_NAME, wifiName);
			cv.put(CONNECTED_TIME, connectTime);
			cv.put(CONNECT_COUNT, connectCount);
			isUpdate = this.mDatabaseAdapter.update(TABLE_NAME, cv, DEVICE_NAME + "= ?", new String[] { deviceName });
		}
		return isUpdate;
	}

	@Override
	public Cursor queryWithWifi(String wifiName) {
		Log.d(TAG, "queryWithWifi ---> wifiName:" + wifiName);
		if (null == wifiName) {
			Log.e(TAG, "queryWithWifi ---> argument(wifiName) is null");
			return null;
		}
		Cursor mCursor = null;
		if (!TextUtils.isEmpty(wifiName)) {
			mCursor = this.mDatabaseAdapter.query(TABLE_NAME, new String[] { KEY_ID, DEVICE_NAME, WIFI_NAME,
					DEVICE_TYPE, CONNECTED_TIME, CONNECT_COUNT }, WIFI_NAME + "= ?", new String[] { wifiName }, null,
					null, CONNECTED_TIME + " DESC");
		}
		return mCursor;
	}

	@Override
	public Cursor queryWithDeviceAndWifi(String deviceName, String wifiName) {
		Log.d(TAG, "queryWithDeviceAndWifi devName:" + deviceName + ", wifiName:" + wifiName);
		if (null == deviceName || null == wifiName) {
			Log.e(TAG, "queryWithWifi ---> arguments is null");
			return null;
		}

		Cursor mCursor = this.mDatabaseAdapter.query(TABLE_NAME, new String[] { KEY_ID, DEVICE_NAME, WIFI_NAME,
				DEVICE_TYPE, CONNECTED_TIME, CONNECT_COUNT }, DEVICE_NAME + "= ? and " + WIFI_NAME + "= ?",
				new String[] { deviceName, wifiName }, null, null, CONNECTED_TIME + " DESC");

		return mCursor;
	}

	@Override
	public Cursor query() {
		// 按照时间降序查询
		Cursor mCursor = this.mDatabaseAdapter.query(TABLE_NAME, new String[] { KEY_ID, DEVICE_NAME, WIFI_NAME,
				DEVICE_TYPE, CONNECTED_TIME, CONNECT_COUNT }, null, null, null, null, CONNECTED_TIME + " DESC");

		return mCursor;
	}

	@Override
	public boolean delete(String deviceName) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 关闭数据库
	 */
	public void close() {
		this.mDatabaseAdapter.close();
	}

	public boolean isLastConnectDevice(String wifiName, String deviceName) {
		Log.d(TAG, "isLastConnectDevice -> wifiName:" + wifiName + ", dev:" + deviceName);

		boolean flag = false;
		Cursor cursor = queryWithWifi(wifiName);
		String saveDeviceName = null;
		int deviceNameIndex = -1;

		if (null != cursor) {
			deviceNameIndex = cursor.getColumnIndex(DEVICE_NAME);
			if (cursor.moveToNext()) {
				saveDeviceName = cursor.getString(deviceNameIndex);
				Log.d(TAG, "isLastConnectDevice -> saveDeviceName:" + saveDeviceName);
				if (null != saveDeviceName && saveDeviceName.equals(deviceName)) {
					flag = true;
				}
			} else {

			}
		} else {
			Log.d(TAG, "cursor is null ");
		}
		Log.d(TAG, "isLastConnectDevice -> " + flag);
		return flag;
	}

	public boolean hasHistoryInWifi(String wifiName) {
		boolean flag = false;
		Cursor cursor = queryWithWifi(wifiName);
		if (null != cursor && cursor.moveToNext()) {
			flag = true;
		}
		Log.d(TAG, "hasHistoryInWifi " + wifiName + " -> " + flag);
		return flag;
	}

	/**
	 * 获取设备连接过的次数
	 * 
	 * @param deviceName
	 * @return
	 */
	public int getDeviceConnectCount(String deviceName, String wifiName) {
		int count = 0;
		Cursor cursor = queryWithDeviceAndWifi(deviceName, wifiName);
		if (null != cursor) {
			int connectCountIndex = cursor.getColumnIndex(CONNECT_COUNT);
			if (cursor.moveToNext()) {
				count = cursor.getInt(connectCountIndex);
			}
			cursor.close();
		}

		return count;
	}

}
